<template>
    <div class="deptbgc">
        <el-header>
            <div class="depttitle">
                岗位管理
            </div>
        </el-header>    
        <div class="btncl">
            <el-button class="deptbtn1" type="primary" @click="addDialogVisible = true">增加岗位</el-button>
            <button class="deptbtn" type="primary" @click="deleteDialogVisible = true">删除岗位</button>
            <button class="deptbtn" type="primary" @click="editDialogVisible = true">修改岗位</button>
            <button class="deptbtn">查询岗位下员工</button>
        </div>

        <el-form ref="form" :model="form">
        <div class="tableborder">
        <el-table :data="StationList" class="tablestyle" :header-cell-style="{background:'#000',color:'gold'}" :row-style="{background:'rgba(0,0,0,0.1)',color:'gold'}">
                <el-table-column property="stnum" label="编号" ></el-table-column>
                <el-table-column property="stna" label="名称" ></el-table-column>
                <el-table-column property="stty" label="岗位类型" ></el-table-column>
                <el-table-column property="storg" label="岗位编制" ></el-table-column>
        </el-table>
        </div>

            <el-dialog title="增加岗位" :visible.sync="addDialogVisible" :append-to-body="true" @close="addStaClosed">
                <el-form :model="query" ref="addStaRef">
                    <el-form-item label="岗位编号" :label-width="formLabelWidth">
                        <el-input v-model="query.stnum"></el-input>
                    </el-form-item>
                    <el-form-item label="岗位名称" :label-width="formLabelWidth">
                        <el-input v-model="query.stna"></el-input>
                    </el-form-item>
                    <el-form-item label="岗位类型" :label-width="formLabelWidth">
                        <el-input v-model="query.stty"></el-input>
                    </el-form-item>
                    <el-form-item label="岗位编制" :label-width="formLabelWidth">
                        <el-input v-model="query.storg"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="addDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addSta">确 定</el-button>
                </div>
            </el-dialog>

            <el-dialog title="删除岗位" :visible.sync="deleteDialogVisible" :append-to-body="true" @close="deleteStaClosed">
                <el-form ref="deleteStaRef">
                <el-form-item label="请输入要删除的岗位编号" :label-width="delWidth" prop="stnum">
                        <el-input v-model="stnum" ></el-input>
                </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="deleteDialogVisible=false">取 消</el-button>
                    <el-button type="primary" @click="deleteSta">确 定</el-button>
                </span>
            </el-dialog>

            <el-dialog title="修改岗位" :visible.sync="editDialogVisible" :append-to-body="true" @close="editStaClosed">
                <el-form :model="query" ref="editStaRef">
                    <el-form-item label="岗位编号" :label-width="formLabelWidth">
                        <el-input v-model="query.stnum"></el-input>
                    </el-form-item>
                    <el-form-item label="岗位名称" :label-width="formLabelWidth">
                        <el-input v-model="query.stna"></el-input>
                    </el-form-item>
                    <el-form-item label="岗位类型" :label-width="formLabelWidth">
                        <el-input v-model="query.stty"></el-input>
                    </el-form-item>
                    <el-form-item label="岗位编制" :label-width="formLabelWidth">
                        <el-input v-model="query.storg"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="editDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="editSta">确 定</el-button>
                </div>
            </el-dialog>
        </el-form>
    </div>
</template>

<script>
export default {
    created() {
        this.getStationList();
    },
    data() {
      return {
        form:{},
        query: {
          stnum: '',
          stna: '',
          stty: '',
          storg:'',
        },
        stnum:"",
        formLabelWidth: '120px',
        delWidth: '180px',
        StationList: [],
        dialogTableVisible: false,
        addDialogVisible: false,
        deleteDialogVisible: false,
        editDialogVisible: false,
      };
    },

    methods:{
        async getStationList(){
            this.$axios.post("http://localhost:8081/getStation", this.query).then(res => {
                console.log(res.data);
                this.StationList = res.data;
            });
        },
        addStaClosed(){
            this.$refs.addStaRef.resetFields();
        },
        deleteStaClosed(){
            this.$refs.deleteStaRef.resetFields();
        },
        editStaClosed() {
            // this.$refs.editStudentRef.resetFields();
        },
        async addSta() {
            this.$refs.addStaRef.validate(async valid => {
                if (!valid) return;
                this.$axios.post("http://localhost:8081/addStation", this.query).then(res => {                        
                    if (res != "success") {
                    this.addDialogVisible = false;
                    this.getStationList();
                    return this.$message.success("操作成功");
                    }
                        this.$message.error("操作失败");
                });
            });
        },
        async deleteSta(stnum) {
            const confirmResult = await this.$confirm('是否确认删除?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).catch(err => err)
            if (confirmResult != 'confirm') {
                return this.$message.info("已取消删除");
            }
            this.$axios.delete("http://localhost:8081/deleteStation?stnum=" + this.stnum).then(res => {
                if(res != "success"){
                    this.getStationList();
                    return this.$message.success("删除成功");
                }
                this.$message.error("删除失败");
            })
        },
        async showEditDialog(stnum) {
            this.$axios.get("http://localhost:8081/upStation?stnum=" + this.stnum).then(res => {
            this.query = res.data;
            this.editDialogVisible = true;
            })
        },
        async editSta() {
            this.$refs.editStaRef.validate(async valid => {
                if (!valid) return;
                this.$axios.post("http://localhost:8081/editStation", this.query).then(res => {
                    if (res != "success") {
                        this.addDialogVisible = false;
                        this.getStationList();
                        return this.$message.success("操作成功");
                    }
                    this.$message.error("操作失败");
                })
            })
        }
    }
  };
</script>

<style scoped>
.deptbgc{
    background: url("../assets/5.jpg");
    width: 100%;
    height: 100%;
    position: fixed;
    margin: -8px;
}

.depttitle{
    font-size: 60px;
    text-align: center;
    color: rgb(216, 185, 7);
    width: 100%;
    height: 80px;
}

.btncl{
    width: 100%;
    height: 132px;
}

.deptbtn1{
    color: gold;
    background-color: #000;
    margin-top: 46px;
    margin-left: 620px;
    font-size: 30px;
    float: left;
    border: 0px;
    cursor: pointer;
}

.deptbtn{
    color: gold;
    background-color: #000;
    margin-top: 50px;
    margin-left: 30px;
    font-size: 30px;
    float: left;
    border: 0px;
    cursor: pointer;
}

.tablestyle{
    background-color: rgba(0,0,0,0.1);
    margin-left: 300px;
    margin-top: 35px;
    width: 1350px;
    height: 600px;
}

.tableborder >>> .el-table__row>td{
  /* 去除表格线 */
  border: none;
}

/* .tableborder >>> .el-table th.is-leaf{
    去除上边框
    border: none;
} */

.el-table::before{
    /* 去除下边框 */
    height: 0px;
}

</style>
